package com.own.component.common.base.entity.po;

import com.baomidou.mybatisplus.annotation.TableField;
import com.fasterxml.jackson.annotation.JsonFormat;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.own.component.common.login.user.BaseLoginUser;
import com.own.constant.ConstantDateFormatPattern;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.*;
import lombok.experimental.FieldNameConstants;

import java.io.Serializable;
import java.time.LocalDateTime;

/**
 * BaseModifiedPo
 * 包含修改信息的表
 *
 * @author chenxueli
 * @date 2022-09-10 12:03:00
 */
@Getter
@Setter
@ToString
@NoArgsConstructor
@AllArgsConstructor
@FieldNameConstants
public class BaseModifiedPo extends BaseCreatePo implements Serializable {

    @Schema(title = "更新人用户名")
    @JsonInclude(JsonInclude.Include.NON_DEFAULT)
    @TableField
    private Long updateUserId;

    @Schema(title = "更新人名称")
    @JsonInclude(JsonInclude.Include.NON_NULL)
    @TableField
    private String updateUserName;

    @Schema(title = "更新时间")
    @JsonInclude(JsonInclude.Include.NON_NULL)
    @JsonFormat(pattern = ConstantDateFormatPattern.Pattern.DATETIME, timezone = "GMT+8")
    @TableField
    private LocalDateTime gmtModified;

    /**
     * 设置初始化信息
     *
     * @param loginUser 登录用户对象
     */
    @Override
    public void init(BaseLoginUser loginUser) {
        super.init(loginUser);
        this.modify(loginUser);
    }

    /**
     * 设置初始化信息
     *
     * @param loginUser 登录用户对象
     */
    public void modify(BaseLoginUser loginUser) {
        this.gmtModified = LocalDateTime.now();
        this.updateUserId = loginUser.userId();
        this.updateUserName = loginUser.userName();
    }

}
